package com.dianping.cat.consumer.dependency.model.transform;

import com.dianping.cat.consumer.dependency.model.IEntity;
import com.dianping.cat.consumer.dependency.model.IVisitor;
import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import java.util.Stack;

public class DefaultMerger
  implements IVisitor
{
/*  14 */   private Stack<Object> m_objs = new Stack();

  private DependencyReport m_dependencyReport;


  public DefaultMerger() {}

  public DefaultMerger(DependencyReport dependencyReport) {
/*  22 */     this.m_dependencyReport = dependencyReport;
/*  23 */     this.m_objs.push(dependencyReport);
  }


/*  27 */   public DependencyReport getDependencyReport() { return this.m_dependencyReport; }



  protected Stack<Object> getObjects() { return this.m_objs; }


  public <T> void merge(IEntity<T> to, IEntity<T> from) {
/*  35 */     this.m_objs.push(to);
    from.accept(this);
/*  37 */     this.m_objs.pop();
  }


/*  41 */   protected void mergeDependency(Dependency to, Dependency from) { to.mergeAttributes(from); }


  protected void mergeDependencyReport(DependencyReport to, DependencyReport from) {
/*  45 */     to.mergeAttributes(from);
/*  46 */     to.getDomainNames().addAll(from.getDomainNames());
  }


/*  50 */   protected void mergeIndex(Index to, Index from) { to.mergeAttributes(from); }



/*  54 */   protected void mergeSegment(Segment to, Segment from) { to.mergeAttributes(from); }



  public void visitDependency(Dependency from) {
/*  59 */     Dependency to = (Dependency)this.m_objs.peek();

/*  61 */     mergeDependency(to, from);
/*  62 */     visitDependencyChildren(to, from);
  }


  protected void visitDependencyChildren(Dependency to, Dependency from) {}


  public void visitDependencyReport(DependencyReport from) {
/*  70 */     DependencyReport to = (DependencyReport)this.m_objs.peek();

/*  72 */     mergeDependencyReport(to, from);
/*  73 */     visitDependencyReportChildren(to, from);
  }

  protected void visitDependencyReportChildren(DependencyReport to, DependencyReport from) {
/*  77 */     for (Segment source : from.getSegments().values()) {
      Segment target = to.findSegment(source.getId());

/*  80 */       if (target == null) {
/*  81 */         target = new Segment(source.getId());
/*  82 */         to.addSegment(target);
      }

/*  85 */       this.m_objs.push(target);
/*  86 */       source.accept(this);
/*  87 */       this.m_objs.pop();
    }
  }


  public void visitIndex(Index from) {
/*  93 */     Index to = (Index)this.m_objs.peek();

/*  95 */     mergeIndex(to, from);
/*  96 */     visitIndexChildren(to, from);
  }


  protected void visitIndexChildren(Index to, Index from) {}


  public void visitSegment(Segment from) {
/* 104 */     Segment to = (Segment)this.m_objs.peek();

/* 106 */     mergeSegment(to, from);
/* 107 */     visitSegmentChildren(to, from);
  }

  protected void visitSegmentChildren(Segment to, Segment from) {
/* 111 */     for (Index source : from.getIndexs().values()) {
/* 112 */       Index target = to.findIndex(source.getName());

/* 114 */       if (target == null) {
/* 115 */         target = new Index(source.getName());
/* 116 */         to.addIndex(target);
      }

/* 119 */       this.m_objs.push(target);
/* 120 */       source.accept(this);
/* 121 */       this.m_objs.pop();
    }

/* 124 */     for (Dependency source : from.getDependencies().values()) {
/* 125 */       Dependency target = to.findDependency(source.getKey());

/* 127 */       if (target == null) {
/* 128 */         target = new Dependency(source.getKey());
/* 129 */         to.addDependency(target);
      }

/* 132 */       this.m_objs.push(target);
/* 133 */       source.accept(this);
/* 134 */       this.m_objs.pop();
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/dependency/model/transform/DefaultMerger.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */